package com.seeyon.apps.ssoAndDoc.utils;

import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.SystemEnvironment;
import com.seeyon.v3x.edoc.domain.EdocSummary;
import com.seeyon.v3x.edoc.manager.EdocSummaryManager;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

/**
 * 正文清稿         
 * 2020.10.22  start-----------wxt.guyinchuan
 * sourcefile 源文件
 * targetfile 目的文件 
 * */  
public class FairCopy {
	private static final Log LOGGER = LogFactory.getLog(FairCopy.class);
	public static boolean FairCopy (Map<String, Object> params) {
		String MarkremoveUrl = AppContext.getSystemProperty("markremove.remove");
		LOGGER.info("TWL-生成清稿文件开始");
		File oldFile = (File) params.get("oldFile");
		String subject = (String) params.get("subject");
		String attachTempPath = (String) params.get("attachTempPath");
		String targetfile = attachTempPath + subject + "--清稿--正文.doc";
		LOGGER.info("TWL-生成清稿文件路径：" + targetfile);
		StringBuffer command = new StringBuffer();
    	command.append(MarkremoveUrl)
    		.append(File.separator)
			.append("markremove")
			.append(" ")
			.append(oldFile.getPath())
			.append(" ")
			.append(targetfile);
		LOGGER.info("清稿-----cmd"+command.toString());
		boolean isSuccess = execCmd(command.toString());
		LOGGER.info("TWL-清稿结果" + isSuccess);
		return isSuccess;
	}
	public static boolean FairCopy (String fileName,String wordname, String summaryId ) {
		LOGGER.info("kaishi清稿-----gu");
		String MarkremoveUrl =AppContext.getSystemProperty("markremove.remove");
//		String MarkremoveUrl = "E:\\Seeyon\\A8\\MarkRemove";
		String  sourcefile =fileName ;
		File edocFromPdfFileFod = new File(getEdocFromFileUrl(summaryId)+summaryId+File.separator);
			if(!edocFromPdfFileFod.exists()) {
				edocFromPdfFileFod.mkdirs();
			}
			EdocSummaryManager edocSummaryManager =(EdocSummaryManager)AppContext.getBean("edocSummaryManager");
			Long summaryid = Long.valueOf(summaryId);
			EdocSummary summary =	edocSummaryManager.findById(summaryid);
			String edocsubject = summary.getSubject();
			String regex = "[><:\"/\\|?*]";
			edocsubject = edocsubject.replaceAll(regex, "");
			edocsubject = edocsubject.replace(" ", "");
		String  targetfile = getEdocFromFileUrl(summaryId)+summaryId+File.separator+edocsubject+"-清稿--正文";
		//获取系统配置查看是什么系统
		String osName = System.getProperty("os.name");
		StringBuffer command = new StringBuffer();
		        	command.append(MarkremoveUrl)
		        							.append("\\markremove")
		        							.append(" ")
		         							.append(sourcefile)
		        							.append(" ")
		        							.append(targetfile);
		    LOGGER.info("清稿-----cmd"+command);
	        boolean isSuccess = execCmd(command.toString());
	        LOGGER.info(command.toString());
	    	return isSuccess;
	}
	
	
	/**
	 * targetfile 目的文件
	 * 2020.10.22  start------------wxt.guyinchuan
	 * */
	public static String getEdocFromFileUrl(String summaryId) {
		String applicationFol = SystemEnvironment.getApplicationFolder();
		String fromFileFol = applicationFol.split("ApacheJetspeed")[0].replaceAll("\\\\", java.util.regex.Matcher.quoteReplacement("\\"))
				+"base"+File.separator+"edocFormFile"+File.separator+summaryId+File.separator;
		return fromFileFol;
	}
	
	/**
	 * 
	 * <p>Title: execCmd
	 * <p>Description: 执行cmd
	 * @author WeiLianTang
	 * @company www.microcental.net
	 * @date 2019年3月13日
	 * @param command
	 * @return
	 */
	private static  boolean execCmd(String command) {
        Process process = null;// Process可以控制该子进程的执行或获取该子进程的信息
        try {
            process = Runtime.getRuntime().exec(command);// exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序，并返回与该子进程对应的Process对象实例。
            // 下面两个可以获取输入输出流
            InputStream errorStream = process.getErrorStream();
            InputStream inputStream = process.getInputStream();
            LOGGER.info("is");
            
            while (errorStream.read() != -1) {
            }
        } catch (IOException e) {
        	e.printStackTrace();
        	return false;
        }
        int exitStatus = 0;
        try {
           exitStatus = process.waitFor();// 等待子进程完成再往下执行，返回值是子线程执行完毕的返回值,返回0表示正常结束
           
           // 第二种接受返回值的方法
           int i = process.exitValue(); // 接收执行完毕的返回值
        } catch (InterruptedException e) {
        	e.printStackTrace();
        	if(process != null) {
        		process.destroy(); // 销毁子进程
                process = null;
        	}
            return false;
        }
        
        if(process != null) {
    		process.destroy(); // 销毁子进程
            process = null;
    	}
        
        if (exitStatus != 0) {
        	return false;
        } else {
        }
        return true;
    }
}
	
	

